﻿//following code utilizes jQuery 1.2.6
var prev = 0;
var scrolling = false;
var bindingTable = "DeltaBindingTable";

$(document).ready(
//DIV showing the message "Loading..." is hidden initially
//The message will be shown when records are fetched with AJAX 
//when user has scrolled to the bottom of the DIV scrollbar
    function () {

        $("tr").filter(function () {
            return $('td', this).length && !$('table', this).length
        }).css({ background: "ffffff" }).hover(
                        function () { $(this).css({ background: "#C1DAD7" }); },
                        function () { $(this).css({ background: "#ffffff" }); }
                        );

        $(".divProgress").hide();

        $(".divData").scroll(

                function () {
                    //triggering point is when the difference of the heights of the TABLE
                    //and DIV match the DIV's scrollTop value
                    if (!scrolling) {
                        var inner = $("#divData > .inner");
                        var elem = $("#divData");
                        if ($("#MainContent_DeltaGridView ").height() - this.scrollTop == $(this).height()) {
                            //if (elem[0].scrollHeight = elem.scrollTop() == elem.outerHeight()){
                            //progress bar         
                            $(".divProgress").ajaxStart(function () {
                                $(this).show();
                            });
                            $(".divProgress").ajaxStop(function () {
                                $(this).hide();
                            });

                            //get last Line Id to track next fetch
                            var LastLineID = $("#MainContent_DeltaGridView  tr:last").children("td:first").html();

                            //get last table row in order to append the new result set increment
                            var trLast = $("#MainContent_DeltaGridView  tr:last");
                            if (parseInt(LastLineID, 10) > parseInt(prev, 10)) {
                                prev = LastLineID;
                                //make a async call to fetch the incremental results
                                $.post("DeltaAsyncHandler.ashx?bindingTable=" + bindingTable + "&lastLineId=" + LastLineID, function (data) {
                                    if (data != null) {
                                        //append new result set to last row
                                        trLast.after(data);
                                    }
                                });
                            }

                        }
                    }
                }
        );
        //        $("#GoToTextBox").blur(
        //            function() {
        //                //progress bar         
        //                $(".divProgress").ajaxStart(function() {
        //                    $(this).show();
        //                });
        //                $(".divProgress").ajaxStop(function() {
        //                    $(this).hide();
        //                });
        //                $(".divData").ajaxStop(function() {
        //                $(this).scrollTop($(this)[0].scrollHeight * .95);
        //                });
        //                //get last Line Id to track next fetch
        //                var LastLineID = $("#MainContent_DeltaGridView  tr:last").children("td:first").html();

        //                //get last table row in order to append the new result set increment
        //                var trLast = $("#MainContent_DeltaGridView  tr:last");
        //                if (parseInt(LastLineID, 10) > parseInt(prev, 10)) {
        //                    prev = LastLineID;
        //                    //make a async call to fetch the incremental results
        //                    $.post("DeltaAsyncHandler.ashx?lastLineId=" + LastLineID + "&nextLineId=" + this.value, function(data) {
        //                        if (data != null) {
        //                            //append new result set to last row
        //                            trLast.after(data);

        //                        }
        //                    });
        //                }


        //            }
        //            );
        $("#GoToLineButton").click(
function () {
    //progress bar         
    $(".divProgress").ajaxStart(function () {
        $(this).show();
    });
    $(".divProgress").ajaxStop(function () {
        $(this).hide();
    });
    $(".divData").ajaxStop(function () {
        //window.alert((this)[0].scrollHeight * .95);
        //        scrolling = true;
        //        $(this).scrollTop($(this)[0].scrollHeight);
        //        scrolling = false;
    });
    //get last Line Id to track next fetch
    //var LastLineID = $("#MainContent_DeltaGridView  tr:last").children("td:first").html();
    //$("#MainContent_DeltaGridView ").empty();
    var LastLineID = 0;
    var NextLineID = $("#GoToTextBox").val();

    //get last table row in order to append the new result set increment
    //var trLast = $("#MainContent_DeltaGridView  tr:last");
    //if (parseInt(LastLineID, 10) > parseInt(prev, 10)) {
    prev = LastLineID;
    //make a async call to fetch the incremental results
    $.post("DeltaAsyncHandler.ashx?bindingTable=" + bindingTable + "&lastLineID=" + LastLineID + "&nextLineID=" + NextLineID, function (data) {
        if (data != null) {
            //append new result set to last row
            $("#MainContent_DeltaGridView ").empty();
            $("#MainContent_DeltaGridView ").append(data);

        }
    }
        );
    //}


}
         );

    }
);
